package name.jiaosha.air.system.controller;

import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.system.domain.Power;
import name.jiaosha.air.system.service.ISystemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;

/**
 * 系统功率
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin/sys")
public class PowerController {

	@Autowired
	private ISystemService systemService;

	@RequestMapping("/powerList")
	public ModelAndView getUnitList() {

		PageInfo<Power> pageInfo = null;

		if (!AppConstants.DEBUG) {
			List<Power> list = new ArrayList<Power>();
			for (int i = 1; i <= 20; i++) {
				Power pwoer = new Power();
				pwoer.setId(i);
				pwoer.setName("1.5P" + i);
				list.add(pwoer);
			}

		} else {

			pageInfo = systemService.getPowerListByComId(1,
					AppConstants.DEFAULT_SIZE);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/createPower")
	public ModelAndView createUnit(@RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			Power power = new Power();
			power.setName(name.trim());
			systemService.createPower(power);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");

		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updatePower")
	public ModelAndView updateUnit(@RequestParam int id,
			@RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			Power power = systemService.getPowerById(id);

			if (power != null) {

				power.setName(name.trim());
				systemService.updatePower(power);
			}

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

}
